package com.bwhx.eruler.util;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class DateUtils {  
	  
	public static Log log = LogFactory.getLog(DateUtils.class);
	
	public static String getTrace(Throwable t){
		
		StringWriter stringWriter= new StringWriter();
		PrintWriter writer= new PrintWriter(stringWriter);
		t.printStackTrace(writer);
		StringBuffer buffer= stringWriter.getBuffer();
		return buffer.toString();
	}
	public static void main(String[] args) {  
	  
		  
		String time = "2016/9/30 15:36:00";  
		  
		  
		log.info(time);  
		  
		  
		// 字符串=======>时间戳  
		  
		  
		Long re_str = getTimeStamp(time);  
		log.info(re_str);  
		  
		  
		// 时间戳======>字符串  
		  
		  
		String data = getStrTime(re_str);  
		log.info(data);  
	
	}  
  
  
// 将字符串转为时间戳  
  
  // "2016/9/30 15:36:00";  
	public static Long getTimeStamp(String user_time) {  
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");  
		Date d; 
		Long l=(long) 0;
		try {  
			  
			d = sdf.parse(user_time);  
			l = d.getTime();  
		  
	  
		} catch (ParseException e) {  
			log.error(getTrace(e));  
		}  
		return l/1000;  
	}  
  
  
	// 将时间戳转为字符串  
	public static String getStrTime(Long cc_time) {  
		String re_StrTime = null;  
		  
		  
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");  
		// 例如：cc_time=1291778220  
		/*long lcc_time = Long.valueOf(cc_time); */ 
		re_StrTime = sdf.format(new Date(cc_time * 1000L));  
		  
		  
		return re_StrTime;  
	  
	  
	}  
	
	public static String now28String() {
		Date date = new Date();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
		String format = simpleDateFormat.format(date);
		return format;
	}
}  